package com.ling.module1sbbyyds.controller.community;

import com.ling.sbbyyds.entity.community.CommunityPostsCollect;
import com.ling.sbbyyds.entity.community.CommunityPostsLike;
import com.ling.sbbyyds.interaction.param.CommunityPostsPo;
import com.ling.module1sbbyyds.service.community.CommunityPostService;
import com.ling.sbbyyds.interaction.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;

/**
 * 天道酬勤、学无止境
 *
 * @Description: 社区帖子
 * @author: ling
 * @date: 2022年03月08日
 */
@RestController
@RequestMapping("api/communityPosts/")
public class CommunityPostsController {

    @Autowired(required = false)
    private CommunityPostService communityPostService;

    /**
     * http://127.0.0.1:8899/api/communityPosts/getCommunityPostsList?page=&limit=
     * 获取话题列表
     *
     * @param page
     * @param limit
     * @param communityPostsPo
     * @return
     */
    @GetMapping("getCommunityPostsList")
    public ResultVo getCommunityPostsList(@RequestParam(value = "page", required = false) Integer page,
                                          @RequestParam(value = "limit", required = false) Integer limit, CommunityPostsPo communityPostsPo) {
        return communityPostService.getCommunityPostsList(page, limit, communityPostsPo);
    }

    /**
     * 发布帖子
     *
     * @param communityPostsPo 帖子参数
     * @param files            图片文件
     * @return 操作反馈
     */
    @PostMapping("isLogin/releaseCommunityPost")
    public ResultVo releaseCommunityPost(@Validated CommunityPostsPo communityPostsPo, @RequestParam(value = "files", required = false) MultipartFile[] files) throws Exception {
        return communityPostService.releaseCommunityPost(communityPostsPo, files);
    }

    /**
     * 发布帖子V2(base64图片数据)
     *
     * @param communityPostsPo 帖子参数
     * @return 操作反馈
     */
    @PostMapping("isLogin/releaseCommunityPostV2")
    public ResultVo releaseCommunityPost(@RequestBody @Validated CommunityPostsPo communityPostsPo) throws Exception {
        return communityPostService.releaseCommunityPost(communityPostsPo);
    }

    /**
     * 获取作者用户所有帖子
     *
     * @param id 用户ID
     * @return 返回用户所有帖子
     */
    @GetMapping("getAuthorPosts")
    public ResultVo getAuthorPosts(Long id) {
        return communityPostService.getAuthorPosts(id);
    }

    /**
     * 获取帖子详情
     *
     * @param postId 帖子id
     * @return
     */
    @GetMapping("getPostDetail/{postId}")
    public ResultVo getPostDetail(@PathVariable Long postId) {
        return communityPostService.getPostDetail(postId);
    }

    /**
     * 获取关注用户发的贴
     *
     * @param page  页数
     * @param limit 条数
     * @return
     */
    @GetMapping("isLogin/getAttUserRePosts")
    public ResultVo getAttUserRePosts(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "limit", defaultValue = "20") Integer limit) {
        return communityPostService.getAttUserRePosts(page, limit);
    }

    /**
     * 点赞帖子
     *
     * @param postsLike 内容
     * @param type      true：点赞  false：取消点赞
     * @return
     */
    @PostMapping("isLogin/likePosts/{type}")
    public ResultVo likePosts(@RequestBody CommunityPostsLike postsLike, @PathVariable Boolean type) {
        return communityPostService.likePosts(postsLike, type);
    }

    /**
     * 收藏帖子
     *
     * @param postsCollect 内容
     * @param type         true：收藏  false：取消收藏
     * @return
     */
    @PostMapping("isLogin/collectPosts/{type}")
    public ResultVo collectPosts(@RequestBody CommunityPostsCollect postsCollect, @PathVariable Boolean type) {
        return communityPostService.collectPosts(postsCollect, type);
    }

    /**
     * 获取用户的所有帖子
     *
     * @param userId   用户ID
     * @param pageCode 页码
     * @param limit    一页数据量
     * @return 分页显示用户的所有帖子
     */
    @GetMapping("getUserPostsList")
    public ResultVo getUserPostsList(@NotBlank(message = "用户标识不能为空") String userId,
                                     Integer pageCode,
                                     Integer limit) {
        return communityPostService.getUserPostsList(userId, pageCode, limit);
    }

    /**
     * 用户更改帖子观看权限
     *
     * @param postId 帖子ID
     * @param status 修改权限状态
     * @return 修改反馈
     */
    @GetMapping("isLogin/updatePostPrivilege")
    public ResultVo updatePostsPrivilege(@NotBlank(message = "帖子标识不能为空") String postId,
                                         @RequestParam(required = false) Integer status) {
        return communityPostService.updatePostsPrivilege(postId, status);
    }

    /**
     * 删除帖子
     * @param postId 帖子ID
     * @return 删除反馈
     */
    @GetMapping("isLogin/deletePosts")
    public ResultVo deletePosts(@NotBlank(message = "帖子标识不能为空") String postId) {
        return communityPostService.deletePosts(postId);
    }
}
